#include<bits/stdc++.h>
#define int long long 
using namespace std;
#define getc() getchar()

inline int read() {
	int x = 0, f = 0; char ch = getc();
	while(ch < '0' or ch > '9') f |= (ch == '-'), ch = getc();
	while(ch >= '0' and ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getc();
	return f ? -x : x;
}
int __stk[128], __top;
inline void write(int x) {
    if(x < 0) putchar('-'), x = -x;
	do { __stk[++__top] = x % 10, x /= 10; } while(x);
	while (__top) putchar(__stk[__top--] + '0');
}
bool stmer;
const int N = 2000;
int n, m;
double w[N];
int nex[N][N];
string t;
string s[N];
void init(int id) {
    int pos = -1; nex[id][0] = -1;
    for(int i = 1; i < s[id].size(); i++) {
        while(pos + 1 and s[id][pos + 1] != s[id][i]) pos = nex[id][pos];
        if(s[id][pos + 1] == s[id][i]) pos++;
        nex[id][i] = pos;
    }
}
int p[N];
char g[N], f[N];
bool edmer;
signed main() {
	cerr << "[Memory] " << (&stmer - &edmer) / 1024 / 1024 << " MB\n";
	freopen("magic.in", "r", stdin);
	// freopen("magic.chk", "w", stdout);
	n = read(), m = read();
	cin >> t;
    for(int i = 1; i <= m; i++) {
        cin >> s[i], w[i] = read();
        w[i] = log2(w[i]);
        init(i);
    }
    fclose(stdin);
    freopen("magic.out", "r", stdin);
    scanf("%s", f);
    for(int i = 1; i <= m; i++) p[i] = -1;
    double ans = 0, tot = 0;
    for(int i = 0; i < n; i++) {
        if(f[i] != t[i] and t[i] != '.') puts("WA"), exit(0);
        for(int j = 1; j <= m; j++) {
            while(p[j] + 1 and s[j][p[j] + 1] != f[i]) p[j] = nex[j][p[j]];
            if(s[j][p[j] + 1] == f[i]) p[j]++;
            if(p[j] == s[j].size() - 1) ans += w[j], p[j] = nex[j][p[j]], tot++;
        }
    } 
    fclose(stdin);
	freopen("magic.ans", "r", stdin);
    // cin >> g;
    // cout << g << '\n';
    scanf("%s", g);
    for(int i = 1; i <= m; i++) p[i] = -1;
    double res = 0, cnt = 0;
    for(int i = 0; i < n; i++) {
        if(g[i] != t[i] and t[i] != '.') puts("ER"), exit(0);
        for(int j = 1; j <= m; j++) {
            while(p[j] + 1 and s[j][p[j] + 1] != g[i]) p[j] = nex[j][p[j]];
            if(s[j][p[j] + 1] == g[i]) p[j]++;
            if(p[j] == s[j].size() - 1) res += w[j], p[j] = nex[j][p[j]], cnt++;
        }
    }
    ans /= tot, res /= cnt;
    cout << ans << '\n' << res << '\n';
    puts(res == ans ? "AC" : "WA");
    cerr << "[Runtime] " << (double) clock() / CLOCKS_PER_SEC << " seconds";
	return 0;
} 